package com.zh.goods.service.impl;


import com.zh.common.domain.UserDO;
import com.zh.common.utils.ShiroUtils;
import com.zh.common.utils.StringUtil;
import com.zh.goods.dao.GoodsDao;
import com.zh.goods.dao.GoodsRecordDao;
import com.zh.goods.dao.GoodsSublistDao;
import com.zh.goods.domain.GoodsDO;
import com.zh.goods.domain.GoodsRecordDO;
import com.zh.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zh.common.utils.ShiroUtils.getUser;
import static com.zh.common.utils.ShiroUtils.getUserId;


@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsSublistDao goodsSublistDao;
	@Autowired
	private GoodsRecordDao goodsRecordDao;
	
	@Override
	public GoodsDO get(String id){
		return goodsDao.get(id);
	}

	@Override
	public List<GoodsDO> get1(String id){
		return goodsDao.get1(id);
	}

	@Override
	public List<GoodsDO> list(Map<String, Object> map){
		return goodsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsDao.count(map);
	}
	
	@Override
	public int save(GoodsDO scGoods){
		String gid = StringUtil.generalKeyword();
		scGoods.setId(gid);
		scGoods.setServicerId(ShiroUtils.getCompanyUser().getCompanyId());//所属公司
		scGoods.setCreateBy(getUserId());
		scGoods.setCreateDate(new Date());
		return goodsDao.save(scGoods);
	}
	
	@Override
	public int update(GoodsDO Goods){
		return goodsDao.update(Goods);
	}

	@Override
	public int updateAndSaveRecord(GoodsDO goodsDO) {
		UserDO userDO = getUser();
		//查询出原有的数量
		GoodsDO scGoodsDO = goodsDao.get(goodsDO.getId());

		//*新增进货记录
		GoodsRecordDO goodsRecord=new GoodsRecordDO();
		goodsRecord.setId(StringUtil.generalKeyword());
		goodsRecord.setRecordDate(new Date());
		//*公司ID
		goodsRecord.setServiceId(scGoodsDO.getServicerId());
		//商品ID
		goodsRecord.setGoodsId(goodsDO.getId());
		//进货数量
		goodsRecord.setNum(goodsDO.getNum());
		BigDecimal num =new BigDecimal(goodsDO.getNum());
		//交易额*
		goodsRecord.setJye(scGoodsDO.getGoodsPrice().subtract(num));
		//单价
		goodsRecord.setDj(goodsDO.getGoodsPrice());
		//总数
		goodsRecord.setAddress(userDO.getUsername());

		goodsRecord.setRemark(goodsDO.getRemark());

		goodsDO.setNum(goodsDO.getNum()+scGoodsDO.getNum());
		if(goodsDao.update(goodsDO)>0){
			if(goodsRecordDao.save(goodsRecord)>0) {
				return 1;
			}
		}
		return 0;
	}

	@Override
	public int remove(String id){
		return goodsDao.remove(id);
	}

	@Override
	@Transactional
	public int removeGoodsAndSubList(String id) {
		if(goodsDao.remove(id)>0){
			if(goodsSublistDao.removeByGoodsId(id)>0) {
				return 1;
			}
		}
		return 0;
	}

	@Override
	public int batchRemove(String[] ids){
		return goodsDao.batchRemove(ids);
	}

	@Override
	@Transactional
	public int batchRemoveGoodsAndSubList(String[] ids) {
		if(goodsDao.batchRemove(ids)>0){
			if(goodsSublistDao.batchRemoveByGoodsId(ids)>0) {
				return 1;
			}
		}
		return 0;
	}

	@Override
	public List<GoodsDO> getAll() {
		return goodsDao.getAll();
	}

}
